define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'usechart/index' + location.search,
                    add_url: 'usechart/add',
                    edit_url: 'usechart/edit',
                    del_url: 'usechart/del',
                    multi_url: 'usechart/multi',
                    table: 'sms',
                }
            });

            var table = $("#table");
            var  stvisible = '=';
            if(Config.org_cate_id > 0){
                stvisible = false;
            }
            table.on('post-common-search.bs.table', function (event, table) {
                $(".commonsearch-table").find('.btn-success').html('搜索');
                $(".commonsearch-table").find('.btn-default').html('刷新');
                $(".columns-right").hide();
                $('.form-commonsearch').attr('autocomplete', 'off');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'days', title:'日期', operate:false, formatter: Table.api.formatter.date},
                        // {field: 'event', title: __('Event')},
                        // {field: 'mobile', title: __('Mobile')},
                        {
                            field: 'orgcateid',
                            title: __('单位'),
                            visible: false,
                            operate:false,
                            align: 'left',
                            // operate:'like',
                            searchList: $.getJSON('usechart/searchlist'),
                            formatter: Controller.api.formatter.cate
                        },
                        // {field: 'code', title: __('Code')},
                        {field: 'total',title: '短信数量', operate:false},
                         {field: 'createtime', title: __('发送时间段'),visible: false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'sms_price_id', title: __('Sms_price_id')},
                        // {field: 'smsprice.id', title: __('Smsprice.id')},
                        // {field: 'smsprice.type', title: __('Smsprice.type')},
                        //
                        // {field: 'createtime', title: __('Smsprice.createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            buttons: [
                                //{
                                //    name: 'detail',
                                //    title: "查看详情",
                                //    text: "查看详情",
                                //    url: 'usechart/detail',
                                //    icon: 'fa fa-table',
                                //    classname: 'btn btn-info btn-xs btn-execute btn-dialog',
                                //    extend: 'data-area=["100%","100%"]'
                                //},
                                {
                                    name: 'detail',
                                    title: __('查看明细'),
                                    text: "查看明细",
                                    classname: 'btn btn-xs btn-warning btn-addtabs btn-ajax',
                                    icon: 'fa fa-folder-o',
                                    url: 'usechart/detail'+ location.search
                                }
                            ],
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                search:false,
                commonSearch:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Table.api.init({
                extend: {
                    chapter_url: 'usechart/detail'+ location.search,
                    table: 'usechart_detail'
                }
            });
            var table1 = $('#table1');
            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.chapter_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('序号'), operate:false},
                        {field: 'createtime', title: __('发送时间'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'content', title: __('发送内容'),operate:false},
                        {field: 'mobile', title: __('收信人'),operate:false},
                        {field: 'orgcate.name', title: __('单位'),visible: false, operate:'like' },
                        {field: 'event', title: __('短信种类'),operate:false, searchList: {"register":__('入驻申请'),"login":__('后台登录'),"reply":__('咨询审核通过'),"orgreply":__('入驻申请审核通过')}, formatter: Table.api.formatter.normal}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                search:false,
                commonSearch:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                cate: function (value, row, index) {
                    console.log(value);
                    console.log(row);
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                }
            }
        }
    };
    return Controller;
});